package com.ruoyi.produce.material.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.produce.material.domain.ProduceMaterialRequisition;
import com.ruoyi.produce.material.domain.ProduceMaterialRequisitionList;
import com.ruoyi.produce.material.service.IProduceMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料领、补、退信息
 *
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.16
 */
@RestController
@RequestMapping("/produce/material")
@Api(value = "物料领、补、退信息模块", tags = "物料领、补、退接口")
public class ProduceMaterialController extends BaseController {

    @Autowired
    private IProduceMaterialService produceMaterialService;


    /**
     * 获取物料领、补、退信息
     * materialRequisitionType：1:领料 2:补料 3:退料（需事先传入）
     *
     * @param produceMaterialRequisition
     * @return
     */
    @ApiOperationSupport(includeParameters = {"ProduceMaterialRequisition.warehouseId", "ProduceMaterialRequisition.materialRequisitionId", "ProduceMaterialRequisition.staffPickingId", "ProduceMaterialRequisition.createTime"})
    @ApiOperation(value = "获取物料领、补、退信息列表",notes = "查询所有物料领、补、退信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProduceMaterialRequisition produceMaterialRequisition) {
        startPage();
        List<ProduceMaterialRequisition> produceMaterialRequisitions = produceMaterialService.selectProduceMaterialRequisitionList(produceMaterialRequisition);
        return getDataTable(produceMaterialRequisitions);
    }

    /**
     * 根据编号获取详细信息
     */
    @GetMapping(value = "/info")
    @ApiOperation(value = "根据编号获取详细信息",notes = "根据编号获取详细信息")
    public AjaxResult getInfo(@RequestParam String materialRequisitionId)
    {
        return AjaxResult.success(produceMaterialService.selectProduceMaterialRequisitionById(materialRequisitionId));
    }

    /**
     * 导出物料领、补、退信息
     *
     * @param produceMaterialRequisition
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody ProduceMaterialRequisition produceMaterialRequisition) {
        List<ProduceMaterialRequisition> produceMaterialRequisitions = produceMaterialService.selectProduceMaterialRequisitionList(produceMaterialRequisition);
        ExcelUtil<ProduceMaterialRequisition> util = new ExcelUtil<ProduceMaterialRequisition>(ProduceMaterialRequisition.class);
        return util.exportExcel(produceMaterialRequisitions, "物料领、补、退信息单数据");
    }

    /**
     * 获取加工单对应物料信息
     * 新增领料单
     *
     * @param produceMaterialRequisition
     * @return
     */
    @PostMapping("/getMaterialRequisitionForReceived")
    public AjaxResult getMaterialRequisitionForReceived(@RequestBody ProduceMaterialRequisition produceMaterialRequisition) {
        List<ProduceMaterialRequisitionList> produceMaterialRequisitionLists = produceMaterialService.getMaterialRequisitionFromWorkSheet(produceMaterialRequisition.getWorkSheetId());

        return AjaxResult.success(produceMaterialRequisitionLists);
    }

    /**
     * 获取加工单对应物料信息
     * 新增退料单
     *
     * @param produceMaterialRequisition
     * @return
     */
    @PostMapping("/getMaterialRequisitionForReturn")
    public AjaxResult getMaterialRequisitionForReturn(@RequestBody ProduceMaterialRequisition produceMaterialRequisition) {
        List<ProduceMaterialRequisitionList> produceMaterialRequisitionLists = produceMaterialService.getMaterialRequisitionFromWorkSheetForReturn(produceMaterialRequisition.getWorkSheetId());

        return AjaxResult.success(produceMaterialRequisitionLists);
    }

    /**
     * 新增物料领、补、退信息
     *
     * @param produceMaterialRequisition 物料领、补、退信息单据
     * @return
     */
    @ApiOperation(value = "新增物料领、补、退信息", notes = "新增物料领、补、退信息")
    @ApiImplicitParam(name = "produceMaterialRequisition", value = "新增物料领、补、退信息", dataType = "ProduceMaterialRequisition")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProduceMaterialRequisition produceMaterialRequisition) {
        if (StringUtils.isNull(produceMaterialRequisition.getProduceMaterialRequisitionLists())) {
            return AjaxResult.error("请选择领料产品！");
        }
        try {
            return toAjax(produceMaterialService.insertProduceMaterialRequisition(produceMaterialRequisition));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 更新物料领、补、退信息
     *
     * @param produceMaterialRequisition 物料领、补、退信息单据
     * @return
     */
    @ApiOperation(value = "更新物料领、补、退信息", notes = "更新物料领、补、退信息")
    @ApiImplicitParam(name = "produceMaterialRequisition", value = "更新物料领、补、退信息", dataType = "ProduceMaterialRequisition")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody ProduceMaterialRequisition produceMaterialRequisition) {
        if (StringUtils.isNull(produceMaterialRequisition.getProduceMaterialRequisitionLists())) {
            return AjaxResult.error("请选择领料产品！");
        }
        try {
            return toAjax(produceMaterialService.updateProduceMaterialRequisition(produceMaterialRequisition));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 领退补料出入库
     * 用于app
     * @param produceMaterialRequisition
     * @return
     */
    @PostMapping("/produceMaterialStorage")
    public AjaxResult produceMaterialStorage(@RequestBody ProduceMaterialRequisition produceMaterialRequisition) {
        return toAjax(produceMaterialService.produceMaterialStorage(produceMaterialRequisition));
    }

    /**
     * 删除物料领、补、退信息
     * 仅修改删除标志位
     *
     * @param produceMaterialRequisition 物料领、补、退信息单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ProduceMaterialRequisition produceMaterialRequisition) {

        return toAjax(produceMaterialService.updateProduceMaterialRequisitionDeleteFlag(produceMaterialRequisition));
    }

}
